﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace CultuRAM.Models
{
    [Table("Events")]
    public class Event
    {
        [Key]
        public int EventId { get; set; }

        [Required(ErrorMessage = "O campo {0} é de preenchimento obrigatório!")] 
        [Display(Name = "Título")]
        public string Title { get; set; }

        [Display(Name = "Cartaz")]
        public string Poster { get; set; }

        //[DataType(DataType.Date, ErrorMessage = "Introduza uma data válida no formato: dd/mm/yyyy hh:mm")]
        //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy HH:mm}")] //dd/MM/yyyy HH:mm
        [Required(ErrorMessage = "O campo {0} é de preenchimento obrigatório!")] 
        [DataType(DataType.Date, ErrorMessage = "Introduza uma data válida no formato: dd/mm/yyyy hh:mm"), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm", ApplyFormatInEditMode = true)]
        [Display(Name = "Data de Início")]
        public DateTime StartDate { get; set; }

        //[DataType(DataType.Date, ErrorMessage = "Introduza uma data válida no formato: dd-mm-yyyy hh:mm")]
        //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy HH:mm}")]
        [Required(ErrorMessage = "O campo {0} é de preenchimento obrigatório!")]
        [DataType(DataType.Date, ErrorMessage = "Introduza uma data válida no formato: dd/mm/yyyy hh:mm"), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm", ApplyFormatInEditMode = true)]
        [Display(Name = "Data de Fim")]
        public DateTime EndDate { get; set; }

        [Display(Name = "Duração")]
        public string Duration { get; set; }

        [Display(Name = "Público")]
        public bool Public { get; set; }

        [Required(ErrorMessage = "O campo {0} é de preenchimento obrigatório!")]
        [Display(Name = "Tipo")]
        public int EventTypeId { get; set; }
        
        [ForeignKey("EventTypeId")]
        [Display(Name = "Tipo")]
        public virtual EventType EventType { get; set; }
       
        [Required(ErrorMessage = "O campo {0} é de preenchimento obrigatório!")]
        [Display(Name = "Entidade")]
        public int EntityId { get; set; }
        
        [ForeignKey("EntityId")]
        [Display(Name = "Entidade")]
        public virtual Entity Entity { get; set; }

       
        public virtual ICollection<EventPropertyValue> PropertyValues { get; set; }
        public virtual IPagedList<EventPropertyValue> PropertyValuesPaged { get; set; }

        public virtual ICollection<Session> EventSessions { get; set; }
        public virtual IPagedList<Session> EventSessionsPaged { get; set; }
        
        public virtual ICollection<EventParticipant> EventParticipants { get; set; }
        public virtual IPagedList<EventParticipant> EventParticipantsPaged { get; set; }
        
        public virtual ICollection<Resource> Resources { get; set; }
        public virtual IPagedList<Resource> ResourcesPaged { get; set; }
        
        public virtual ICollection<Comment> Comments { get; set; }
        public virtual IPagedList<Comment> CommentsPaged { get; set; }

        public IPagedList<EventType> ListEventTypes { get; set; }
        public IPagedList<Entity> ListEntities { get; set; }
        
        //Check if has Property
        public bool HasProperty( String aPropertyName )
        {
            return PropertyValues != null && PropertyValues.Any(property => property.EventProperty.Name == aPropertyName);
        }

        //Get the value of Property
        public string GetPropertyValue( String aPropertyName )
        {
            if (PropertyValues != null )
            {
                foreach ( var property in PropertyValues )
                {
                    if ( property.EventProperty.Name != aPropertyName ) continue;
                    return property.Value;
                }
            }
            return String.Empty;
        }

     
        //Get the city of Place
        public string GetCityValue(String aPlaceName, IEnumerable<Place> aPlaces)
        {
            foreach (var place in aPlaces)
            {
                if (place.NamePlace != aPlaceName) continue;
                return place.City.CityName;
            }
            return String.Empty;
        }
        
        //Get the Coordenate of Place
        public string GetCoordinatesPlace(String aPlaceName, IEnumerable<Place> aPlaces)
        {
            foreach (var place in aPlaces)
            {
                if (place.NamePlace != aPlaceName) continue;
                return place.Coordinates;
            }
            return String.Empty;
        }


        //Check if Property has value
        public bool HasPropertyValue( String aPropertyName, String aPropertyValue )
        {
            if ( ! HasProperty( aPropertyName ) ) return false;
            return aPropertyValue.ToLower() == GetPropertyValue( aPropertyName ).ToLower();
        }

        //Check if Participant exist
        public bool HasParticipant(String aParticipantName)
        {
            if (String.IsNullOrEmpty(aParticipantName)) return true;
           
            if (EventParticipants.Count != 0)
            {
                foreach (var particip in EventParticipants)
                {
                   if (particip.Participant.Name.ToLower() == aParticipantName.ToLower()) return true;       
                }
            }
     
            return false;
        }

        //Check if Participant Type exist
        public bool HasParticipantType(String aParticipantType)
        {
            if (String.IsNullOrEmpty(aParticipantType)) return true;

            if (EventParticipants.Count != 0)
            {
                foreach (var particip in EventParticipants)
                {
                    if (particip.Participant.ParticipantType.ParticipType.ToLower() == aParticipantType.ToLower()) return true;
                }
            }

            return false;
        }

        //Check if Property value is Null Or Equal
        public bool HasPropertyNullOrEqual( String aPropertyName, String aPropertyValue )
        {
            if (!HasProperty(aPropertyName) && !String.IsNullOrEmpty(aPropertyValue)) return false;
            if (String.IsNullOrEmpty(aPropertyValue)) return true;
            return aPropertyValue.ToLower() == GetPropertyValue( aPropertyName ).ToLower();
        }

        //Compare City Name
        public bool HasCityValue(String aCityName1, String aCityName2)
        {
            if (String.IsNullOrEmpty(aCityName2)) return true;
            return aCityName1.ToLower().Equals(aCityName2.ToLower());

        }
        
    }
}